package com.example.text.ui.mall.viewmodel

import androidx.lifecycle.MutableLiveData
import com.example.text.base.BaseViewModel
import com.example.text.base.state.Failed
import com.example.text.base.state.Loading
import com.example.text.base.state.RequestEvent
import com.example.text.base.state.Success
import com.example.text.ui.mall.model.GoodEntity
import com.example.text.ui.mall.model.HomeGoodsCategory
import com.example.text.ui.mall.model.MallBanner
import com.example.text.ui.mall.model.MallGoods
import com.example.text.ui.mall.repository.MallRepository
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow

/** 商城首页相关 */
open class BaseMallViewModel(
    private val mRepository: MallRepository
) : BaseViewModel() {



    protected val _stateListener = MutableSharedFlow<RequestEvent>()
    val stateListener get() = _stateListener.asSharedFlow()
    val mallBannersLiveData by lazy {
        MutableLiveData<ArrayList<MallBanner>>()
    }
    val goodsListLiveData by lazy {
        MutableLiveData<ArrayList<MallGoods>>()
    }

    /** 首页车型 */
    val goodsCategoryLiveData by lazy {
        MutableLiveData<ArrayList<HomeGoodsCategory>>()
    }
    val mSeriesGoods by lazy {
        MutableLiveData<MallGoods?>()
    }

    fun cmallAdInfo(adType: String, onSuccess: ((ad: MallBanner?) -> Unit)? = null){
        execute(
            action = mRepository.cmallAdInfo(),
            onStart = {
                //开始请求
                _stateListener.emit(Loading)
            },
            onError = {
                //请求失败
                _stateListener.emit(Failed(it))
            },
            onFinish = {
                _stateListener.emit(Success)
//                if (onSuccess != null) {
//                    onSuccess.invoke(it.data?.firstOrNull())
//                } else {
                    mallBannersLiveData.postValue(it.data as ArrayList<MallBanner>?)

                //Log.d("ddd", GSON.toJson(it.data))
               // }

            }
        )
    }
    fun goodsList(adType: String, onSuccess: ((ad: MallBanner?) -> Unit)? = null){
        execute(
            action = mRepository.GoodsList(),
            onStart = {
                //开始请求
                _stateListener.emit(Loading)
            },
            onError = {
                //请求失败
                _stateListener.emit(Failed(it))
            },
            onFinish = {
                _stateListener.emit(Success)
//                if (onSuccess != null) {
//                    onSuccess.invoke(it.data?.firstOrNull())
//                } else {

                goodsListLiveData.postValue(it.data as ArrayList<MallGoods>?)

                //Log.d("ddd", GSON.toJson(it.data))
                // }

            }
        )
    }

    fun cMallGroupList(type: String = "c_home") {
//        val param = MallGroupBody(type)
        execute(
            action = mRepository.GoodsList(),
            onStart = { _stateListener.emit(Loading) },
            onError = {
                //请求失败
                _stateListener.emit(Failed(it))
            },
            onFinish = {
                _stateListener.emit(Success)

                goodsCategoryLiveData.postValue(it.data as ArrayList<HomeGoodsCategory>?)

            }
        )
    }

    /** 通用商品信息 */
    val commGoodsInfo by lazy {
        MutableLiveData<MallGoods>()
    }
    /** c端-龙豆商品详情查询接口 */
    fun pointGoodsInfo(goodsId: Long) {
        execute(action = mRepository.pointGoodsInfo(goodsId = goodsId),
            onStart = { _stateListener.emit(Loading) },
            onError = {
                //请求失败
                _stateListener.emit(Failed(it))
            },
            onFinish = {
                _stateListener.emit(Success)
                if (it.data?.id == null || it.data.id == 0L) {
                    _stateListener.emit(Failed("商品已下架"))
                } else {
                    commGoodsInfo.postValue(it.data  as MallGoods?)
                }



            }
        )
    }

//    /** c端-商品详情-推荐 */
//    fun mallGoodsInfoInfo(
//        id: Long, onSuccess: ((goods: MallGoods?) -> Unit)? = null,
//        onFailed: ((msg: String?) -> Unit)? = null
//    ) {
//        executeOpenApi(action = mRepository.mallGoodsInfoInfo(CommonSimpleBody(id)),
//            onStart = { _stateListener.emit(Loading) },
//            onApiError = {
//                _stateListener.emit(Failed(it.detail))
//                onFailed?.invoke(it.detail)
//            },
//            onRequestError = {
//                _stateListener.emit(Failed(it.detail))
//                onFailed?.invoke(it.detail)
//            },
//            onFinished = {
//                _stateListener.emit(Success)
//                if (it.mallGoodsInfoInfo?.id == null || it.mallGoodsInfoInfo.id == 0L) {
//                    _stateListener.emit(Failed("商品已下架"))
//                    onFailed?.invoke("商品已下架")
//                } else {
//                    commGoodsInfo.postValue(it.mallGoodsInfoInfo)
//                    onSuccess?.invoke(it.mallGoodsInfoInfo)
//                }
//            })
//    }








}